﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DemoGeometry
{
    /// <summary>
    /// 方程基类
    /// </summary>
    public abstract class Equation
    {
        public float A { get; set; }
        public float B { get; set; }
        public float C { get; set; }

        /// <summary>
        /// 判断是否有效
        /// </summary>
        /// <returns></returns>
        public abstract bool IsValid();

        /// <summary>
        /// 通过Y值获取x值
        /// </summary>
        /// <param name="y"></param>
        /// <returns></returns>
        public abstract float GetValueFromY(float y);
        /// <summary>
        /// 通过X获取Y值
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public abstract float GetValueFromX(float x);
    }

    /// <summary>
    /// 直线方程类一般式:Ax+By+C=0(A、B不同时为0)【适用于所有直线】
    /// </summary>
    public class LinearEquation:Equation
    {
        /// <summary>
        /// 通过X值得到Y值
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public override float GetValueFromX(float x)
        {
            if (B == 0)
            {
                return float.MaxValue;
            }
            return -A * x * 1.0f / B - C * 1.0f / B;
        }

        public override float GetValueFromY(float y)
        {
            if (A == 0)
            {
                return float.MaxValue;
            }
            return -B * y * 1.0f / A - C * 1.0f / A;
        }

        /// <summary>
        /// 判断是否有效方程
        /// </summary>
        /// <returns></returns>
        public override bool IsValid()
        {
            bool flag = true;
            if (A == 0 && B == 0)
            {
                flag = false;
            }
            return flag;
        }

        public override string ToString()
        {
            return string.Format("{0}x+{1}y+{2}=0", A, B, C);
        }
    }

    /// <summary>
    /// 抛物线方程表达式 y=ax2+bx+c
    /// </summary>
    public class ParabolicEquation:Equation
    {

        /// <summary>
        /// 判断是否有效的方程
        /// </summary>
        /// <returns></returns>
        public override bool IsValid()
        {
            //A 不得等于0
            return A != 0;
        }

        /// <summary>
        /// 通过X值得到Y值
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public override float GetValueFromX(float x)
        {
            double y = A * Math.Pow(x, 2) + B * x + C;
            return float.Parse(y.ToString());
        }

        public override float GetValueFromY(float y)
        {
            return 0.0f;
        }
    }
}
